package com.hk.player;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;


import com.hk.player.core.IPlayerControl;
import com.zhoug.common.utils.ClickDebounce;
import com.zhoug.zmui.ZMUIAutoHideLinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 视频控制按钮组件
 * @Author: zhoug
 * @Date: 2024-07-24
 * @Description:
 */
public class PlayerControlView extends FrameLayout implements IPlayerControl {



    private int flag = FLAG_ALL;
    /**
     * 录像按钮
     */
    private ImageView mBtnRecording;
    /**
     * 截图按钮
     */
    private ImageView mBtnScreenshot;
    /**
     * 对讲按钮
     */
    private ImageView mBtnTalk;
    /**
     * 巡检按钮
     */
    private ImageView mBtnWrite;
    /**
     * 回放按钮
     */
    private ImageView mBtnPlayback;
    /**
     * 质量:高清表情按钮
     */
    private TextView mBtnQuality;
    /**
     * 声音 按钮
     */
    private ImageView mBtnSound;
    /**
     * 全屏按钮
     */
    private ImageView mBtnFullScreen;
    /**
     * 正在录像
     */
    private RecordVideoTimeView mRecordVideoView;



    public PlayerControlView(@NonNull Context context) {
        this(context, null);
    }

    public PlayerControlView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PlayerControlView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        View root = LayoutInflater.from(getContext()).inflate(R.layout.hk_player_control_view, this, false);
        addView(root);
        mBtnRecording = root.findViewById(R.id.video_ui_record_video);
        mBtnScreenshot = root.findViewById(R.id.video_ui_capture_image);
        mBtnTalk = root.findViewById(R.id.video_ui_talk);
        mBtnWrite = root.findViewById(R.id.video_ui_write_record);
        mBtnPlayback = root.findViewById(R.id.video_ui_play_back);
        mBtnQuality = root.findViewById(R.id.video_ui_quality);
        mBtnSound = root.findViewById(R.id.video_ui_sound);
        mBtnFullScreen = root.findViewById(R.id.video_ui_fullscreen);
        mRecordVideoView = root.findViewById(R.id.recordVideoView);

        mBtnQuality.setVisibility(GONE);
    }





    /**
     * 设置按钮的显示状态
     * @param view 按钮
     * @param flag
     */
    private void setVisibility(View view, int flag) {
        boolean show = (this.flag & flag) == flag;
        if(show){
            view.setVisibility(VISIBLE);
        }else{
            view.setVisibility(GONE);
        }
        if(view.getLayoutParams() instanceof ZMUIAutoHideLinearLayout.LayoutParams layoutParams){
            layoutParams.autoVisibility=show;
            view.setLayoutParams(layoutParams);
        }
    }


    /**
     * {@link #FLAG_RECORDING}
     * @param flag
     */
    @Override
    public void setBtnFlag(int flag) {
        if (this.flag != flag) {
            this.flag = flag;
            //录像按钮
            setVisibility(mBtnRecording,FLAG_RECORDING);
            //截图按钮
            setVisibility(mBtnScreenshot,FLAG_SCREEN_SHOT);
            //对讲按钮
            setVisibility(mBtnTalk,FLAG_TALK);
            //巡检按钮
            setVisibility(mBtnWrite,FLAG_WRITE);
            //回放按钮
            setVisibility(mBtnPlayback,FLAG_PLAYBACK);
            //质量:高清表情按钮
            setVisibility(mBtnQuality,FLAG_QUALITY);
            //声音按钮
            setVisibility(mBtnSound,FLAG_SOUND);
            //全屏按钮
            setVisibility(mBtnFullScreen,FLAG_FULL_SCREEN);
        }
    }

    @Override
    public void setRecordBtnState(boolean recording) {
        if (recording) {
            mRecordVideoView.start();
            mBtnRecording.setImageResource(R.drawable.hk_player_icon_recording_video);
        } else {
            mRecordVideoView.stop();
            mBtnRecording.setImageResource(R.drawable.hk_player_icon_record_video);
        }
    }

    @Override
    public void setTalkBtnState(boolean talking) {
        if (talking) {
            mBtnTalk.setImageResource(R.drawable.hk_player_icon_talking);
        } else {
            mBtnTalk.setImageResource(R.drawable.hk_player_icon_talk);
        }
    }

    @Override
    public void setMuteBtnState(boolean mute) {
        if (mute) {
            mBtnSound.setImageResource(R.drawable.hk_player_icon_mute_open);
        } else {
            mBtnSound.setImageResource(R.drawable.icon_video_mute_close);
        }
    }

    @Override
    public void setFullScreenBtnState(boolean fullScreen) {
        if (fullScreen) {
            mBtnFullScreen.setImageResource(R.drawable.hk_player_icon_fullscreen_no);
        } else {
            mBtnFullScreen.setImageResource(R.drawable.hk_player_icon_fullscreen_yes);
        }
    }

    @Override
    public void setRecordBtnClickListener(OnClickListener onClickListener) {
        mBtnRecording.setOnClickListener(ClickDebounce.wrap(onClickListener));
    }

    @Override
    public void setScreenshotBtnClickListener(OnClickListener onClickListener) {
        mBtnScreenshot.setOnClickListener(ClickDebounce.wrap(onClickListener));
    }

    @Override
    public void setTalkBtnClickListener(OnClickListener onClickListener) {
        mBtnTalk.setOnClickListener(ClickDebounce.wrap(onClickListener));
    }

    @Override
    public void setWriteBtnClickListener(OnClickListener onClickListener) {
        mBtnWrite.setOnClickListener(ClickDebounce.wrap(onClickListener));
    }

    @Override
    public void setPlaybackBtnClickListener(OnClickListener onClickListener) {
        mBtnPlayback.setOnClickListener(ClickDebounce.wrap(onClickListener));
    }

    @Override
    public void setQualityBtnClickListener(OnClickListener onClickListener) {
        mBtnQuality.setOnClickListener(ClickDebounce.wrap(onClickListener));
    }

    @Override
    public void setMuteBtnClickListener(OnClickListener onClickListener) {
        mBtnSound.setOnClickListener(ClickDebounce.wrap(onClickListener));
    }

    @Override
    public void setFullScreenBtnClickListener(OnClickListener onClickListener) {
        mBtnFullScreen.setOnClickListener(ClickDebounce.wrap(onClickListener));
    }


    public ImageView getBtnRecording() {
        return mBtnRecording;
    }

    public ImageView getBtnScreenshot() {
        return mBtnScreenshot;
    }



}
